import requests
import base64
from io import BytesIO
from PIL import Image

# 1. 加载并编码本地图片为 base64
image_path = "./1.jpeg"
with open(image_path, "rb") as image_file:
    image_base64 = base64.b64encode(image_file.read()).decode("utf-8")

# 2. 构造请求数据
prompt = "描述图片，使用中文"
payload = {
        "model": "./qwen2.5_vl_7B", # 模型名称（与 serve 时指定的路径无关，仅用于标识）
        "prompt": prompt,
        # "images": [image_base64], # 图片以 base64 格式传递
        "max_tokens": 512, # 生成的最大 token 数
        "temperature": 0.7
}

# 3. 发送 POST 请求
url = "http://localhost:8000/v1/completions"
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)

# 4. 处理响应
if response.status_code == 200:
    result = response.json()
    generated_text = result["choices"][0]["text"]
    print("模型输出:", generated_text)
else:
    print("请求失败:", response.status_code, response.text)
